﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RooHui.OA.Biz;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.Entity;

namespace RooHui.OA.Web.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/

        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult GetUserListByPage(int page, int pageSize, string userName = "", string realName = "")
        {
            int count = 0;
            var userList = UserBiz.GetUserListByPage(page, pageSize, userName, ref count);

            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("Rows", userList);
            dic.Add("Total", count);
            return Content(dic.ToJson());
        }

       

        [HttpPost]
        public ActionResult UserSave(string userName, string realName, string telePhone, string handPhone, int optype)
        {
            if (optype == 1)
            {
                var user = UserBiz.EditUser(userName, realName, telePhone, handPhone);

                if (user == null)
                {
                    return Content("0");
                }
                else
                {
                    return Content("1");
                }
            }
            else
            {
                if (UserBiz.UserExsit(userName))
                {
                    return Content("-1");
                }

                var user = UserBiz.AddUser(userName, realName, telePhone, handPhone, System.Web.HttpContext.Current.User.Identity.Name);
                if (user == null)
                {
                    return Content("0");
                }
                else
                {
                    return Content("1");
                }
            }
        }
 

        [HttpPost]
        public ActionResult UserDelete(string userNames)
        {
            foreach (string userName in userNames.Split(','))
            {
                UserBiz.DeleteUser(userName);
            }

            return Content("1");
        }

        [HttpPost]
        public ActionResult UserChangeStatus(string userName, int status)
        {

            var user = UserBiz.ChangeStatus(userName, status);

            if (user == null)
            {
                return Content("0");
            }
            else
            {
                return Content("1");
            }
        }
         [HttpPost]
        public ActionResult GetRoleByUserName(string userName)
        {
            var list = UserRoleBiz.GetUserRoleByUserName(userName);
            if (list != null && list.Count > 0)
            {

                List<Dictionary<string, string>> datalist = new List<Dictionary<string, string>>();
                foreach (var item in list)
                {
                    var data = new Dictionary<string, string>();
                    data.Add("ID", item.role.ID.ToString());
                    data.Add("Name", item.role.Name);
                    data.Add("ParentID", "0");
                    if (item.RoleID == 0)
                    {
                        data.Add("ischecked", null);
                    }
                    else
                    {
                        data.Add("ischecked", "true");
                    }

                    datalist.Add(data);
                }
                return Content(datalist.ToJson());
            }
            return Content("");
        }

         [HttpPost]
         public ActionResult UserAuthorize(string userName, string roleIDs)
         {
             if (UserRoleBiz.UserAuthorizeDelete(userName))
             {
                 UserRoleBiz.UserAuthorize(userName, roleIDs);
                 return Content("1");
             }
             return Content("0");

         }

         [HttpPost]
         public ActionResult UserAuthorizeDelete(string userName)
         {
             if (UserRoleBiz.UserAuthorizeDelete(userName))
             {
                 return Content("1");
             }
             return Content("0");
         }

         [HttpPost]
         public ActionResult GetActiveUser(string key="")
         {
             var userlist = UserBiz.GetActiveUserList(key);
             return Content(userlist.ToJson());
         } 

    }
}
